package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.AreaBean;
import com.emip.dao.AreaDAO;
import com.emip.service.AreaService;

@Service("areaService")
public class AreaServiceImpl implements AreaService{

	@Autowired
	AreaDAO areaDAO;
	
	public List<AreaBean> getAll() {		
		return areaDAO.getAll();
	}

	public AreaBean getAreaByID(int id) {
		return areaDAO.findByID(id);
	}

	public AreaBean addArea(String areaID, String province, String city,
			String district, String postCode) {
		AreaBean newBean = new AreaBean();
		newBean.setAreaID(areaID);
		newBean.setProvince(province);
		newBean.setCity(city);
		newBean.setDistrict(district);
		newBean.setPostCode(postCode);
		//查重，是否已经有相同的areaID的记录存在
		//若有重复记录，则返回null
		if (areaDAO.findExist(newBean)) {
			return null;
		}
		int newID = areaDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
	}

	public AreaBean updateArea(int id, String areaID, String province, String city,
			String district, String postCode) {
		
		AreaBean newBean = areaDAO.findByID(id);
		newBean.setAreaID(areaID);
		newBean.setProvince(province);
		newBean.setCity(city);
		newBean.setDistrict(district);
		newBean.setPostCode(postCode);
		//查重，是否已经有相同的areaID的记录存在
		//若有重复记录，则返回null
		if (areaDAO.findExist(newBean)) {
			return null;
		}
		areaDAO.update(newBean);
		return newBean;
	}

	public Boolean deleteArea(int id) {
		AreaBean delBean = areaDAO.findByID(id);
		areaDAO.remove(delBean);
		return true;
	}

}
